(function() {
    'use strict';
    angular.module('webApp')
        .controller('WifiCtrl', WifiCtrl);

    WifiCtrl.$inject = ['$rootScope', '$scope', 'dataService', '$timeout', '$location', 'helper', '$document', '$filter'];

    function WifiCtrl($rootScope, $scope, dataService, $timeout, $location, helper, $document, $filter) {
        $scope.indexAction = function() {
            $scope.shops = [];
            $scope.$watch("gzh_appid", function(newV, oldV) {
                if (newV) {
                    $scope.shopGet();
                }
            });
        }

        $scope.shopAction = function() {
            $scope.shop_id = $location.$$search.shop_id;
            $scope.gzh_appid = $location.$$search.gzh_appid;
            $scope.store_id = $rootScope.store_id;
            if (!$scope.shop_id || !$scope.gzh_appid) {
                return helper.myAlert("非法参数", "#/wifi/index");
            }

            $scope.wifiShop().then(function() {
                $scope.shopDevice();
                dataService.gzhWxopenWxamplink($scope.gzh_appid);
            });
        }

        //保存顶部文字类型
        $scope.barTypeSave = function() {
            dataService.gzhWifiBarSet($scope.gzh_appid, $scope.shop_id, $scope.bar_type).then(function() {
                helper.myAlert("设置成功！");
                $scope.wifiShop();
            });
        }

        //门店信息
        $scope.wifiShop = function() {
            return dataService.gzhWifiShop($scope.gzh_appid, $scope.shop_id).then(function(shopinfo) {
                if (shopinfo) {
                    $timeout(function() {
                        $scope.shopinfo = shopinfo;
                        $scope.bar_type = $scope.shopinfo.bar_type;
                    });
                }
            });
        }

        // 添加wifi设备
        $scope.wifiAdd = function() {
            if (!$scope.ssid) {
                return helper.myAlert("wifi不能为空");
            }
            if (!($scope.ssid.indexOf("WX") === 0 || $scope.passwd.indexOf("WX") === 0)) {
                return helper.myAlert("wifi名称或者密码必须有1个是WX开头才行", null, 999);
            }

            dataService.gzhWifiDeviceAdd($scope.gzh_appid, $scope.shop_id, $scope.ssid, $scope.passwd).then(function(a) {
                if (a) {
                    dataService.gzhWifiQrcodeGet($scope.gzh_appid, $scope.shop_id, $scope.ssid).then(function(b) {
                        if (b) {
                            helper.myAlert("<div style='text-align:center;'><img src='" + b + "' style='width:240px;' ><br>微信扫描上面的二维码成功连接无线后，才算加入成功！</div>", null, 9999, function() {
                                $scope.shopDevice();
                                $scope.wifiShop();
                            });
                        }
                    });
                }
            });
        }

        // 设备二维码
        $scope.wifiQrcode = function(ssid, shop_id) {
            shop_id = shop_id || $scope.shop_id;
            dataService.gzhWifiQrcodeGet($scope.gzh_appid, shop_id, ssid).then(function(b) {
                if (b) {
                    helper.myAlert("<div style='text-align:center;'><img src='" + b + "' style='width:240px;' ><br>鼠标右键保存图片可以打印粘贴店内</div>", null, 9999, function() {
                        $scope.shopDevice();
                    });
                }
            });
        }

        // 删除wifi设备
        $scope.wifiDel = function(ssid) {
            helper.myConfirm("确认删除？", function() {
                dataService.gzhWifiDeviceDelete($scope.gzh_appid, ssid).then(function(a) {
                    helper.myAlert("删除成功！");
                    $scope.shopDevice();
                    $scope.wifiShop();
                });
            });
        }

        // 门店wifi设备
        $scope.shopDevice = function() {
            dataService.gzhWifiDevice($scope.gzh_appid, $scope.shop_id).then(function(re) {
                $scope.devices = re.records;
            });
        }
        // 门店wifi设备
        $scope.shopWifiClean = function() {
            helper.myConfirm("确认情况所有门店wifi？会导致现有加入二维码失效。", function() {
                dataService.gzhWifiShopClean($scope.gzh_appid, $scope.shop_id).then(function(re) {
                    helper.myAlert("清空成功！");
                });
            })
        }

        // 店铺查询
        $scope.shopGet = function() {
            return dataService.gzhWifiShopList($scope.gzh_appid).then(function(a) {
                if (a) {
                    $timeout(function() {
                        $scope.shops = a.records;
                        if (a.records.length == 0) {
                            $scope.auth().then(function() {
                                if ($scope.wifi_is_open) {
                                    helper.myAlert("你还没有创建wifi门店，<a href='https://mp.weixin.qq.com' target='_blank'>点这里登录公众号</a>，按照下图添加门店<br>" + '<a href="https://o2o.gtimg.com/offline/bizportal/images/mendian.jpg" target="_blank"><img src="https://o2o.gtimg.com/offline/bizportal/images/mendian.jpg" style="width:98%;" /></a>', null, 999);
                                }
                            });
                        }
                    });
                }

            });
        }

        $scope.auth = function() {
            return dataService.wifiToken($scope.gzh_appid).then(function(res) {
                console.error("res", res);
                if (!res.is_open) {
                    $scope.authUrl = res.auth_url;
                    //helper.myAlert("你还没有开通wifi插件，<a href='"+res.auth_url+"' target='_blank'>点这里开通</a><br>"+'<img src="https://img.alicdn.com/imgextra/i4/20841398/TB2Oq3Dn3fH8KJjy1zcXXcTzpXa-20841398.png" style="width:90%;"/>',null,999);
                    helper.myAlert("你还没有开通wifi插件，<a href='https://mp.weixin.qq.com' target='_blank'>点这里登录公众号</a>，按照下图添加wifi插件<br>" + '<a href="https://img.alicdn.com/imgextra/i4/20841398/TB2Oq3Dn3fH8KJjy1zcXXcTzpXa-20841398.png" target="_blank"><img src="https://img.alicdn.com/imgextra/i4/20841398/TB2Oq3Dn3fH8KJjy1zcXXcTzpXa-20841398.png" style="width:98%;" /></a>', null, 999);
                } else {
                    $scope.wifi_is_open = true;
                }
            });
        }
        $scope.device = function() {
            dataService.wifiDeviceList($scope.gzh_appid).then(function(res) {
                console.error("res", res);
            });
        }

        $scope.saveAuth = function(data) {
            console.error("data", data);
        }
    }
})();